/*! Vertex Buffer Direct3D 11 Implementation

@author Sergio J. de los Santos
*/
#include <stdafx.h>

#include "VertexBuffer.h"

//! Constructor
VertexBufferD3D11::VertexBufferD3D11(ID3D11Device* pd3dDevice)
{
	m_pd3dDevice		= pd3dDevice;
	m_pVB				= NULL;
}

//! Destructor
VertexBufferD3D11::~VertexBufferD3D11(void)
{
	Destroy();
}

/*! Libera todos los objetos de uso interno

@return true if ok, false if failed
*/
bool VertexBufferD3D11::Destroy()
{
	_SAFE_RELEASE(m_pVB);
	return true;
}

/*! Inicializa el Vertex Buffer

@param streamInfo	Informacion necesaria para crear el vertex stream
@param pVertices	Vertices a subir al vertex buffer (es opcional salvo si el vertex buffer es inmutable)
@return true if ok, false if failed
*/
bool VertexBufferD3D11::Create(int vertexSize, int vertexCount, const void* pVertices)
{
	// Si no hay Dispositivo D3D
	if(!m_pd3dDevice)	return false;

	D3D11_USAGE d3dUsage	= D3D11_USAGE_IMMUTABLE;
	uint32		uiAccess	= 0;

	D3D11_BUFFER_DESC bufferDesc;
	bufferDesc.Usage			= d3dUsage; // Los estaticos sin posibilidad de Update deberian ser D3D11_USAGE_IMMUTABLE
	bufferDesc.ByteWidth		= vertexCount * vertexSize;
	bufferDesc.BindFlags		= D3D11_BIND_VERTEX_BUFFER;
	bufferDesc.CPUAccessFlags	= uiAccess;
	bufferDesc.MiscFlags		= 0;

	D3D11_SUBRESOURCE_DATA InitData;
	InitData.pSysMem			= pVertices;
	InitData.SysMemPitch		= 0;
	InitData.SysMemSlicePitch	= 0;	

	HRESULT hr = m_pd3dDevice->CreateBuffer( &bufferDesc, &InitData, &m_pVB );
	if( FAILED( hr ) )
		return false;

#ifdef DEBUG
	const char szName[] = "VertexBuffer";
	m_pVB->SetPrivateData( WKPDID_D3DDebugObjectName, sizeof( szName ) - 1, szName );
#endif 

	m_pVB->GetDesc(&m_descBuffer);

	return true;
}


